package cn.org.xuanguang.kepler.hormos.rdb.dml.operator.delete;

import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.DmlOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dsl.execute.Delete;
import cn.org.xuanguang.kepler.hormos.rdb.dsl.param.Term;
import cn.org.xuanguang.kepler.hormos.rdb.dsl.param.TermType;
import cn.org.xuanguang.kepler.hormos.rdb.dsl.sql.SqlRequest;
import cn.org.xuanguang.kepler.hormos.rdb.executor.SyncSqlExecutor;
import cn.org.xuanguang.kepler.hormos.rdb.plugin.DmlSQLBuilder;

import java.util.Collection;

public class DeleteOperator implements DmlOperator<DeleteOperatorResulet> {
    // SyncSqlExecutor用于同步执行SQL命令
    private SyncSqlExecutor syncSqlExecutor;

    // dailatype用于指定数据库类型，影响数据库操作的执行方式
    private String dailatype;
    /**
     * 入参
     */
    DeleteOperatorParameter parameter;

    /**
     * 结果
     *
     * @return
     */
    DeleteOperatorResulet result = new DeleteOperatorResulet();


    public DeleteOperator(SyncSqlExecutor syncSqlExecutor, String dailatype, DeleteOperatorParameter parameter) {
        this.syncSqlExecutor = syncSqlExecutor;
        this.dailatype = dailatype;
        this.parameter = parameter;

    }

    public static DeleteOperator of(SyncSqlExecutor syncSqlExecutor, String dailatype, DeleteOperatorParameter parameter) {
        return new DeleteOperator(syncSqlExecutor, dailatype, parameter);
    }


    /**
     * 构造函数
     *
     * @param parameter
     */
    public DeleteOperator(DeleteOperatorParameter parameter) {
        this.parameter = parameter;
    }


    // 输入表名
    public DeleteOperator table(String tableName) {
        parameter.setTableName(tableName);
        return this;
    }

    // 查询条件
    public DeleteOperator where(Delete delete) {
        parameter.getTerms().addAll(delete.getParam().getTerms());
        return this;
    }

    public DeleteOperator where(Collection<? extends Term> terms) {
        parameter.getTerms().addAll(terms);
        return this;
    }

    public DeleteOperator and(String column, String termType, Object value) {
        parameter.getTerms().addAll(Delete.of().and(column, termType, value).getParam().getTerms());
        return this;
    }

    /**
     * 默认是=
     * @param column
     * @param value
     * @return
     */
    public DeleteOperator and(String column,  Object value) {
        parameter.getTerms().addAll(Delete.of().and(column, TermType.eq, value).getParam().getTerms());
        return this;
    }

    public DeleteOperator or(String column, String termType, Object value) {
        parameter.getTerms().addAll(Delete.of().or(column, termType, value).getParam().getTerms());
        return this;
    }
    /**
     * 默认是=
     * @param column
     * @param value
     * @return
     */
    public DeleteOperator or(String column,  Object value) {
        parameter.getTerms().addAll(Delete.of().or(column, TermType.eq, value).getParam().getTerms());
        return this;
    }
    /**
     * 获取sql
     *
     * @return
     */
    @Override
    public SqlRequest getSql() {
        // 获取sql
        return DmlSQLBuilder.of(dailatype).buildDelete(parameter);

    }

    @Override
    public DeleteOperatorResulet execute() {
        int updated = syncSqlExecutor.update(getSql());
        result.setUpdate(updated);
        return result;
    }
}
